<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./swiper.css">
  <title>图片轮播图</title>
</head>
<body>
  <div class="main" id="main">
    <!-- 二级菜单 -->
    <div class="menu-box"></div>
    <!-- 子菜单 -->
    <div class="sub-menu hide" id="sub-menu">
      <!-- 手机 -->
      <div class="inner-box">
        <div class="sub-inner-box">
          <div class="title">手机、配件</div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
        </div>
      </div>
      <!-- 电脑 -->
      <div class="inner-box">
        <div class="sub-inner-box">
          <div class="title">电脑</div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
          <div class="sub-row">
            <span class="bold mr10">手机通讯</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
            <span class="ml10 mr10">/</span>
            <a href="">手机</a>
          </div>
        </div>
      </div>
    </div>
    <!-- 主菜单 -->
    <div class="menu-content" id="menu-content">
      <div class="menu-item">
        <a href=""><span>手机、配件</span><i class="icon-more"></i></a>
      </div>
      <div class="menu-item">
        <a href=""><span>电脑</span><i class="icon-more"></i></a>
      </div>
      <div class="menu-item">
        <a href=""><span>家用电器</span><i class="icon-more"></i></a>
      </div>
      <div class="menu-item">
        <a href=""><span>家具</span><i class="icon-more"></i></a>
      </div>
    </div>
    <!-- 焦点图 -->
    <div class="banner" id="banner">
      <a href="">
        <div class="banner-slide slide1 slide-active"></div>
      </a>
      <a href="">
        <div class="banner-slide slide2"></div>
      </a>
      <a href="">
        <div class="banner-slide slide3"></div>
      </a>
    </div>
    <a href="javascript:;" class="button prev" id="prev"></a>
    <a href="javascript:;" class="button next" id="next"></a>
    <div class="dots" id="dots">
      <span class="active"></span>
      <span></span>
      <span></span>
    </div>
  </div>
  <script>
    var index = 0,
        timer = null,
        prev = byId('prev'),
        next = byId('next'),
        main = byId('main'),
        menuContent = byId('menu-content'),
        menuItems = menuContent.getElementsByClassName('menu-item'),
        pics = byId("banner").getElementsByTagName('div'),
        dots = byId('dots').getElementsByTagName('span')
        size = pics.length;


    // 鼠标滑过主菜单
    for(var m = 0, mlen = menuItems.length; m<mlen; m++){
      addHandler(menuItems[m], "mouseenter", function(){
        console.log(11111)
      })
    }

    for(var d =0; d<size; d++){
      (function(d){
        addHandler(dots[d], 'click', function(){
          changeImg(d)
        })
      })(d);
    }

    startAutoPlay()
    function startAutoPlay(){
      timer = setInterval(function(){
        if(++index >= size) index = 0;
        changeImg(index)
      }, 3000)
    }
    function stopAutoPlay(){
      if(timer){
        clearInterval(timer)
      }
    }
    addHandler(main, 'mouseover', stopAutoPlay)
    addHandler(main, 'mouseout', startAutoPlay)
    addHandler(next, 'click', function(){
      if(++index >= size) index = 0;
      changeImg(index)
    })

    addHandler(prev, 'click', function(){
      if(--index < 0) index = size-1;
      changeImg(index)
    })

    
    function changeImg(index){
      for(var i = 0; i<size; i++){
        pics[i].style.display = 'none'
        dots[i].className = ''
      }
      pics[index].style.display = 'block'
      dots[index].className = 'active'
    }

    // 封装getElementById
    function byId(id){
      return typeof id === 'string' ? document.getElementById(id) : id
    }
    // 封装添加元素事件
    function addHandler(el, type, handler){
      if(el.addEventListener){
        el.addEventListener(type, handler, false)
      } else if(el.attachEvent){
        el.attachEvent('on'+type, handler)
      } else {
        el['on' + type] = handler
      }
    }

    // 点击下一张按钮
    // DOM0级事件绑定，只能给一个元素的某一个行为绑定一次方法，第二次绑定的会把前面的覆盖
    // DOM2级事件绑定，可以绑定多个不同的方法
    // 第三个参数 true在捕获阶段调用 false在冒泡阶段调用 attachEvent detachEvent

    // next['onclick'] = function(){
    //   console.log(111)
    // }
  </script>
</body>
</html>